//
//  ViewController.m
//  Share
//
//  Created by apple on 16/3/23.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *labelForContent;
@property (nonatomic, strong) CLLocationManager *locationManager;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.labelForContent.lineBreakMode = 0;
    self.labelForContent.numberOfLines = 0;
    
    UIWebView *web = [[UIWebView alloc] init];
    
    NSString *tsw = @"%E9%80%89%E6%8B%A9";
    
    NSString *sc = [NSString stringWithFormat:@"decodeURIComponent('%@')",tsw];
    
    NSString *st = [web stringByEvaluatingJavaScriptFromString:sc];
    
    NSLog(@"%@", st);
    

    
}
- (IBAction)handleButton:(id)sender {
    self.locationManager = [[CLLocationManager alloc]init];
    _locationManager.delegate = self;
    
    //控制定位精度,越高耗电量越大。
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    _locationManager.distanceFilter = kCLDistanceFilterNone;
//     _locationManager.distanceFilter = 100; //控制定位服务更新频率。单位是“米”
    //开始定位
    [_locationManager startUpdatingLocation];
    //在ios 8.0下要授权
    //两种调用方式
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        [_locationManager requestAlwaysAuthorization];
//    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
//        [_locationManager requestWhenInUseAuthorization];  //调用了这句,就会弹出允许框了.
//        始终允许访问位置信息
//        NSLocationAlwaysUsageDescription
//        使用应用程序期间允许访问位置数据
//        NSLocationWhenInUseUsageDescription
    
}

// 错误信息
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"error");
}

#pragma mark - 地址调用代理方法
// 6.0 以上调用这个函数
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *newLocation = locations[0];
    CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
    NSLog(@"旧的经度：%f,旧的纬度：%f",oldCoordinate.longitude,oldCoordinate.latitude);
    //    CLLocation *newLocation = locations[1];
    //    CLLocationCoordinate2D newCoordinate = newLocation.coordinate;
    //    NSLog(@"经度：%f,纬度：%f",newCoordinate.longitude,newCoordinate.latitude);
    // 计算两个坐标距离
    //    float distance = [newLocation distanceFromLocation:oldLocation];
    //    NSLog(@"%f",distance);
    [manager stopUpdatingLocation];
    //------------------位置反编码---5.0之后使用-----------------
    //反向地理编码
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //通过编码返回位置
    [geocoder reverseGeocodeLocation:newLocation
                   completionHandler:^(NSArray *placemarks, NSError *error){
                       NSMutableString *str = @"".mutableCopy;
                       for (CLPlacemark *place in placemarks) {
                           
                           NSLog(@"country,%@",place.country);                 // 国家
                           NSLog(@"administrativeArea,%@", place.administrativeArea); //省
                           NSLog(@"locality,%@",place.locality);               // 市
                           NSLog(@"subLocality,%@",place.subLocality);         // 区
                           
                           NSLog(@"name,%@",place.name);                       // 位置名
                           NSLog(@"thoroughfare,%@",place.thoroughfare);       // 街道
                           NSLog(@"subThoroughfare,%@",place.subThoroughfare); // 子街道
                           [str appendFormat:@"%@ %@ %@ %@ %@ %@ %@",place.country, place.administrativeArea, place.locality, place.subLocality, place.name, place.thoroughfare, place.subThoroughfare];
                           self.labelForContent.text = str;
                           
                       }
                       
                       
                   }];
 
    
}

// 6.0 调用此函数
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
